package top.cwk.dubbo.web.router;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
 * @ClassName FieldUtil
 * @Description 参数赋值
 * @Author 维康
 * @Date 2018/7/27 17:38
 * @Version 1.0
 **/
public class FieldUtil {

    /**
     * set属性的值到Bean
     * @param beanTypeName
     * @param valMap
     */
    public static Object setFieldValue(String beanTypeName, Map<String, String> valMap) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> cls = Class.forName(beanTypeName);
        Object bean = cls.newInstance();
        // 取出bean里的所有方法
        Method[] methods = cls.getDeclaredMethods();
        Field[] fields = cls.getDeclaredFields();
        for (Field field : fields) {
            try {
                String fieldSetName = parSetName(field.getName());
                if (!checkSetMet(methods, fieldSetName)) {
                    continue;
                }
                Method fieldSetMet = cls.getMethod(fieldSetName, field.getType());
                String value = valMap.get(field.getName());
                if (null != value && !"".equals(value)) {
                    String fieldType = field.getType().getSimpleName();
                    if (FieldTypeConstants.TYPE_STRING.equals(fieldType)) {
                        fieldSetMet.invoke(bean, value);
                    } else if (FieldTypeConstants.TYPE_DATE.equals(fieldType)) {
                        Date temp = parseDate(value);
                        fieldSetMet.invoke(bean, temp);
                    } else if (FieldTypeConstants.TYPE_INTEGER.equals(fieldType) || FieldTypeConstants.TYPE_INT.equals(fieldType)) {
                        Integer intval = Integer.parseInt(value);
                        fieldSetMet.invoke(bean, intval);
                    } else if (FieldTypeConstants.TYPE_LONG.equalsIgnoreCase(fieldType)) {
                        Long temp = Long.parseLong(value);
                        fieldSetMet.invoke(bean, temp);
                    } else if (FieldTypeConstants.TYPE_DOUBLE.equalsIgnoreCase(fieldType)) {
                        Double temp = Double.parseDouble(value);
                        fieldSetMet.invoke(bean, temp);
                    } else if (FieldTypeConstants.TYPE_BOOLEAN.equalsIgnoreCase(fieldType)) {
                        Boolean temp = Boolean.parseBoolean(value);
                        fieldSetMet.invoke(bean, temp);
                    } else if (FieldTypeConstants.TYPE_FLOAT.equalsIgnoreCase(fieldType)) {
                        Float temp = Float.parseFloat(value);
                        fieldSetMet.invoke(bean, temp);
                    } else {
                        System.err.println("not supper type" + fieldType);
                    }
                }
            } catch (Exception e) {
                continue;
            }
        }
        return bean;
    }

    /**
     * 判断是否存在某属性的 set方法
     * @param methods
     * @param fieldSetMet
     * @return boolean
     */
    private static boolean checkSetMet(Method[] methods, String fieldSetMet) {
        for (Method met : methods) {
            if (fieldSetMet.equals(met.getName())) {
                return true;
            }
        }
        return false;
    }
    /**
     * 判断是否存在某属性的 get方法
     * @param methods
     * @param fieldGetMet
     * @return boolean
     */
    private static boolean checkGetMet(Method[] methods, String fieldGetMet) {
        for (Method met : methods) {
            if (fieldGetMet.equals(met.getName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * 拼接某属性的 get方法
     * @param fieldName
     * @return String
     */
    private static String parGetName(String fieldName) {
        if (null == fieldName || "".equals(fieldName)) {
            return null;
        }
        return "get" + fieldName.substring(0, 1).toUpperCase()
                + fieldName.substring(1);
    }
    /**
     * 拼接在某属性的 set方法
     * @param fieldName
     * @return String
     */
    private static String parSetName(String fieldName) {
        if (null == fieldName || "".equals(fieldName)) {
            return null;
        }
        return "set" + fieldName.substring(0, 1).toUpperCase()
                + fieldName.substring(1);
    }

    /**
     * 格式化string为Date
     * @param datestr
     * @return date
     */
    public static Date parseDate(String datestr) {
        if (null == datestr || "".equals(datestr)) {
            return null;
        }
        String fmtstr = null;
        if (datestr.indexOf(':') > 0) {
            fmtstr = "yyyy-MM-dd HH:mm:ss";
        } else {
            fmtstr = "yyyy-MM-dd";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(fmtstr, Locale.UK);
        try {
            return sdf.parse(datestr);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     * 日期转化为String
     * @param date
     * @return date string
     */
    public static String fmtDate(Date date) {
        if (null == date) {
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.SIMPLIFIED_CHINESE);
        return sdf.format(date);
    }


}
